package chenjun.mydatabindingdemo.activity;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;

import java.util.ArrayList;
import java.util.List;

import chenjun.mydatabindingdemo.R;
import chenjun.mydatabindingdemo.adapter.VpAdapter;
import chenjun.mydatabindingdemo.fragment.JokeByTimeFragment;
import chenjun.mydatabindingdemo.fragment.JokeImgFragment;
import chenjun.mydatabindingdemo.fragment.JokeImgLastFragment;
import chenjun.mydatabindingdemo.fragment.JokeLastFragment;
import chenjun.mydatabindingdemo.fragment.RandomJokeFragment;
import chenjun.mydatabindingdemo.util.Logger;

public class MainActivity extends BaseActivity {

    private ViewPager vpMain;
    private TabLayout tabMain;
    private List<Fragment> fragments;
    private FragmentManager fm;
    private List<String> mTitles;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setAdapter();
    }

    @Override
    protected void setView() {
        setContentView(R.layout.activity_main);
    }

    private void setAdapter() {
        Logger.i((vpMain == null) + "---" + (fm == null)+"---"+(fragments == null));
        vpMain.setAdapter(new VpAdapter(fm,fragments));
        tabMain.setupWithViewPager(vpMain);
        for (int i = 0; i < fragments.size(); i++){
            TabLayout.Tab tab = tabMain.getTabAt(i);
            tab.setText(mTitles.get(i));
        }

    }

    @Override
    protected void setListener() {

    }

    @Override
    protected void initViews() {
        Logger.i("initViews");
        vpMain = (ViewPager) findViewById(R.id.vp_main);
        tabMain = (TabLayout) findViewById(R.id.tab_main);
        Logger.i((vpMain == null)+"");
    }

    @Override
    protected void initData() {
        fragments = new ArrayList<>();
        fragments.add(JokeLastFragment.newInstance());
        fragments.add(JokeImgLastFragment.newInstance());
        fragments.add(JokeByTimeFragment.newInstance());
        fragments.add(JokeImgFragment.newInstance());
        fragments.add(RandomJokeFragment.newInstance());

        mTitles = new ArrayList<>();
        mTitles.add("开心查找");
        mTitles.add("最热趣图");
        mTitles.add("最新笑话");
        mTitles.add("趣图查找");
        mTitles.add("试试手气");

        fm = getSupportFragmentManager();
    }
}
